/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Outils;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 *
 * @author Val
 */
public class Outil {
    /*public static Object xParse(String url,String xPathExpression) throws XPathExpressionException {

        List<String> res = new ArrayList<String>();
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();

        domFactory.setNamespaceAware(true); // never forget this!

            DocumentBuilder builder;
            Object result = null;
            XPathExpression expr = null;
            Document doc = null;

        try {
            builder = domFactory.newDocumentBuilder();
            doc = builder.parse(url);

            XPathFactory factory = XPathFactory.newInstance();

            XPath xpath = factory.newXPath();
            expr = xpath.compile(xPathExpression);

            result = expr.evaluate(doc, XPathConstants.NODESET);
        }
        catch (ParserConfigurationException ex) {
            Logger.getLogger(Outil.class.getName()).log(Level.SEVERE, null, ex);
            return xParse(url, xPathExpression);
        }
        catch(XPathException e) {
            try {
                result = expr.evaluate(doc, XPathConstants.STRING);
            }
            catch (XPathExpressionException ex) {
                result = expr.evaluate(doc, XPathConstants.NUMBER);
            }
        }
        catch(SAXException e) {
            result = null;
        }
        catch(IOException e) {
            result = null;
        }

        return result;
    }*/
    private static Object parse(String url,String xpathurl, QName dataType) throws XPathExpressionException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        Object result = null;
        XPathExpression expr = null;
        Document doc = null;
        try {
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            doc = builder.parse(url);
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            expr = xpath.compile(xpathurl);
            result = expr.evaluate(doc, dataType);
        }
        catch(SAXException ex) {
            Logger.getLogger(Outil.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(IOException ex) {
            Logger.getLogger(Outil.class.getName()).log(Level.SEVERE, null, ex);
        }catch (ParserConfigurationException ex) {
            Logger.getLogger(Outil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public static NodeList xParseNodeList(String url,String xpathurl) throws XPathExpressionException {
        return (NodeList) parse(url, xpathurl, XPathConstants.NODESET);
    }
    public static String xParseString(String url,String xpathurl) throws XPathExpressionException {
        return (String) parse(url, xpathurl, XPathConstants.STRING);
    }
    public static double xParseNumber(String url,String xpathurl) throws XPathExpressionException {
        return (Double) parse(url, xpathurl, XPathConstants.NUMBER);
    }

}
